<script setup>
import { ref } from 'vue';
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom,
    DocumentCopy,
    Document,
    FolderChecked,
    DataBoard,
    DataAnalysis,
    DataLine,
    TakeawayBox,
    CollectionTag,
    Help,
    Mouse,
    PriceTag
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

import { userInfoService } from '@/api/userApi/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import { useTokenStore } from '@/stores/token.js'
import { useProjectStore } from '@/stores/projectStore';

const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
const projectStore = useProjectStore();
const isAdmin = ref(false); // 用于判断是否为管理员

//调用函数,获取用户详细信息
const getUserInfo = async () => {
    //调用接口
    let result = await userInfoService();
    //数据存储到pinia中
    userInfoStore.setInfo(result.data);
    // 判断是否为管理员
    isAdmin.value = result.data.permission === 'admin';
}

getUserInfo();
//条目被点击后,调用的函数
import { useRouter } from 'vue-router'
const router = useRouter();
import { ElMessage, ElMessageBox } from 'element-plus'
const handleCommand = (command) => {
    //判断指令
    if (command === 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '您确认要退出吗?',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //退出登录
                //1.清空pinia中存储的token以及个人信息
                tokenStore.removeToken()
                userInfoStore.removeInfo()
                projectStore.removeSelectedProject()
                //2.跳转到登录页面
                router.push('/login')
                ElMessage({
                    type: 'success',
                    message: '退出登录成功',
                })

            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '用户取消了退出登录',
                })
            })
    } else {
        //路由
        router.push('/user/' + command)
    }
}
</script>

<template>
    <!-- element-plus中的容器 -->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <!-- element-plus的菜单标签 -->
            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <el-sub-menu index="1">
                    <template #title>
                        <el-icon>
                            <Management />
                        </el-icon>
                        <span>接口信息管理</span>
                    </template>
                    <el-menu-item index="/iim/projects">
                        <el-icon><Mouse /></el-icon>
                        <span>项目管理</span>
                    </el-menu-item>
                    <el-menu-item :index="projectStore.selectedProject.name ? '/iim/objects' : null" :disabled="!projectStore.selectedProject.name">
                        <el-icon><Help /></el-icon>
                        <span>对象管理</span>
                    </el-menu-item>
                    <el-menu-item :index="projectStore.selectedProject.name ? '/iim/requirements' : null" :disabled="!projectStore.selectedProject.name">
                        <el-icon><CollectionTag /></el-icon>
                        <span>需求管理</span>
                    </el-menu-item>
                    <el-menu-item :index="projectStore.selectedProject.name ? '/iim/interfaces' : null" :disabled="!projectStore.selectedProject.name">
                        <el-icon><TakeawayBox /></el-icon>
                        <span>接口管理</span>
                    </el-menu-item>
                    <el-menu-item :index="projectStore.selectedProject.name ? '/iim/elements' : null" :disabled="!projectStore.selectedProject.name">
                        <el-icon><PriceTag /></el-icon>
                        <span>元素管理</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>

            <el-menu v-if="isAdmin" active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <el-sub-menu index="2">
                    <template #title>
                        <el-icon>
                            <Promotion />
                        </el-icon>
                        <span>基本信息管理</span>
                    </template>
                    <el-menu-item index="/bi/interface">
                        <el-icon><DataLine /></el-icon>
                        <span>接口类型</span>
                    </el-menu-item>
                    <el-menu-item index="/bi/io">
                        <el-icon><DataAnalysis /></el-icon>
                        <span>I/O类型</span>
                    </el-menu-item>
                    <el-menu-item index="/bi/specifications">
                        <el-icon><DataBoard /></el-icon>
                        <span>数据规格</span>
                    </el-menu-item>
                    <el-menu-item index="/bi/data">
                        <el-icon><FolderChecked /></el-icon>
                        <span>数据类型</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>

            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <el-sub-menu index="3">
                    <template #title>
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>建立文档</span>
                    </template>
                    <el-menu-item index="/cd/interface">
                        <el-icon><Document /></el-icon>
                        <span>接口文档</span>
                    </el-menu-item>
                    <el-menu-item index="/cd/requirement">
                        <el-icon><DocumentCopy /></el-icon>
                        <span>需求文档</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>

            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <el-sub-menu index="4">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>用户管理</span>
                    </template>
                    <el-menu-item v-if="isAdmin" index="/user/userManage">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>用户管理</span>
                    </el-menu-item>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>信息修改</span>
                    </el-menu-item>
                    <el-menu-item index="/user/resetPassword">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>密码修改</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>用户：<strong>{{ userInfoStore.info.name ? userInfoStore.info.name : "请先登录！！！" }}</strong></div>
                <div>当前所选项目：<strong>{{ projectStore.selectedProject.name ? projectStore.selectedProject.name : "请先选择一个项目！！！" }}</strong></div>

                <!-- 下拉菜单 -->
                <!-- command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <!-- <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item> -->
                            <el-dropdown-item command="resetPassword" :icon="EditPen">修改密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>需求分析平台 ©2025 Created by XAUT</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/mylogo.png') no-repeat center / 200px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>